define(function () {
    'use strict';

    function ctrl($scope,$stateParams,$ionicHistory,$ionicLoading,$timeout,LocalCacheService,InfoService, WebService) {

        $scope.id = $stateParams["id"];
        $scope.econtract_id = $stateParams["econtract_id"];

        $scope.$on('$ionicView.loaded', function() {
            init();
        });

        function init(){
            if($scope.econtract_id &&  $scope.econtract_id.length > 0) {

                WebService.getEncontract($scope.econtract_id).then(function (data) {
                    var file_content = data.econtract_content;
                    data.econtract_content = null;
                    $scope.agreement = data;
                    $scope.agreement.file_name  = $scope.agreement.econtract_name;

                    showContent(file_content);
                });
            } else {
                InfoService.getProdFileContent($scope.id).then(function (data){
                    var file_content = data.file_content;
                    data.file_content = null;
                    $scope.agreement = data;

                    showContent(file_content);
                });
            }
        }

        function showContent(file_content){
            $ionicLoading.show();
            $timeout(function(){
                var utel = angular.element(document.querySelector('#file_content'));
                utel.html(file_content);
                $ionicLoading.hide();
            }, 600);
        }

        $scope.closeAgreementContent = function(){
            var agreeAgreements = LocalCacheService.get("agreeAgreements");
            if(!agreeAgreements) {
                agreeAgreements = "";
            }
            if(agreeAgreements.indexOf($scope.id) == -1) {
                agreeAgreements += "," + $scope.id;
            }
            LocalCacheService.set("agreeAgreements", agreeAgreements);
            $ionicHistory.goBack();
        }
    }

    ctrl.$inject = ['$scope','$stateParams','$ionicHistory','$ionicLoading','$timeout','LocalCacheService','InfoService', 'WebService'];
    return ctrl;
});
